package info.pello.games.libgdx.pong;


import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;

public class PongGame implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	private Texture texturaPala;
	private TextureRegion regionPala;
	private Texture texturaPelota;
	private TextureRegion regionPelota;
	private World world;
	Vector3 puntoDeToque;
	
	@Override
	public void create() {		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		puntoDeToque = new Vector3();
		
		world = new World();
		camera = new OrthographicCamera(10, 15);
		camera.position.set(10f / 2, 15f / 2, 0);
		
		batch = new SpriteBatch();

		// Cargamos la textura de la pala
		texturaPala = new Texture(Gdx.files.internal("data/pala.jpg"));
		texturaPala.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		regionPala = new TextureRegion(texturaPala, 0,0,64,8);
		
		// Cargamos la textura de la pelota
		texturaPelota = new Texture(Gdx.files.internal("data/pelota.jpg"));
		
		texturaPelota.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		regionPelota = new TextureRegion(texturaPelota, 0,0,16,16);


		camera.update();
		batch.setProjectionMatrix(camera.combined);
	}

	@Override
	public void dispose() {
		batch.dispose();
		texturaPala.dispose();
		texturaPelota.dispose();
	}

	@Override
	public void render() {		
		if (Gdx.input.isTouched()){
			camera.unproject(puntoDeToque.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			if(puntoDeToque.y <= 7)
			world.getPala().mover(puntoDeToque.x - 1);
			else{
				world.getPala2().mover(puntoDeToque.x - 1);
			}
		}
		/*
		 if(Gdx.input.justTouched()) {
		      Vector3 touchPos = new Vector3();
		      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
		      camera.unproject(touchPos);
		      Gdx.app.log("Log", "Han tocado en el game: " + Gdx.input.getX() + ":" + Gdx.input.getY());
				if(btnLeft.contains(touchPos)){
				      world.kitty.left();
				      Gdx.app.log("Log", "Han tocado left: " + world.kitty.x + ":" + world.kitty.y);
				}
				
				if(btnRight.contains(touchPos)){
				      world.kitty.right();
				      Gdx.app.log("Log", "Han tocado right: " + world.kitty.x + ":" + world.kitty.y);
				}
		     // setScreen(new HelloKittyGame(this.game));
		   }
		   */
		 world.update();
		 
		// Borramos la pantalla ponemos todo a NEGRO
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
		batch.setProjectionMatrix(camera.combined);
		
		batch.enableBlending();
		
		batch.begin();
		
		//sprite.draw(batch);
		//batch.draw(Assets.background, 0, 0, 15, 10);
		//batch.draw(Assets.lft, 0, 0, 1, 1);
		//batch.draw(Assets.right, 14, 0, 1, 1);
		//batch.draw(texturaPala, 2,3);
		//batch.draw(texturaPelota, 0,0);
		batch.draw(regionPelota, world.getPelota().posicion.x,world.getPelota().posicion.y,0.5f,0.5f);
		batch.draw(regionPala, world.getPala().posicion.x,world.getPala().posicion.y,3,0.5f);
		batch.draw(regionPala, world.getPala2().posicion.x,world.getPala2().posicion.y,3,0.5f);
	    // Gdx.app.log("Log", "Fruit rain: " +":"+ fruit.fruitName +":"+ fruit.position.x + ":" + fruit.position.y);
		//batch.draw(Assets.kitty, world.kitty.x, 0,1f,1.5f);
		batch.end();

	}

	@Override
	public void resize(int width, int heigcht) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
